package com.example.controller;

import com.example.dto.EmpDTO;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.EmpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/emps")
@RequiredArgsConstructor
public class EmpController {

    private final EmpService empService;

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询员工，id:{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("根据ID删除员工，id:{}", id);
        empService.removeById(id);
        return Result.success();
    }

    //@RequestBody注解用于将JSON数据封装到对象中
    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        log.info("新增员工：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    //条件分页查询
    @GetMapping
    public Result find(EmpDTO empDTO) {

        log.info("条件分页参数：{}", empDTO);
        //获取条件分页查询结果
        PageBean<Emp> pageBean = empService.find(empDTO);

        return Result.success(pageBean);
    }

    //修改员工信息
    @PutMapping
    public Result updateById(@RequestBody Emp emp) {
        log.info("修改员工信息：{}", emp);
        empService.updateEmp(emp);
        return Result.success();
    }
}
